package com.lottery.material.service.admin.conf.impl;

import com.lottery.material.dao.conf.PlatformScaleConfigMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.enumeration.conf.PlatformScaleConfigEnum;
import com.lottery.material.pojo.conf.PlatformScaleConfig;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.conf.PlatformScaleConfigCustom;
import com.lottery.material.service.admin.conf.PlatformScaleConfigService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import com.lottery.material.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Description：系统平台收益分成比例配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 17:15
 */
@Service("adminPlatformScaleConfigService")
public class PlatformScaleConfigServiceImpl implements PlatformScaleConfigService {

    private final PlatformScaleConfigMapper platformScaleConfigMapper;

    @Autowired
    public PlatformScaleConfigServiceImpl(PlatformScaleConfigMapper platformScaleConfigMapper) {
        this.platformScaleConfigMapper = platformScaleConfigMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param platformScaleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(PlatformScaleConfig platformScaleConfig) {
        platformScaleConfig.setCreateTime(new Date());
        platformScaleConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue()); //默认未逻辑删除
        //平台占比
        String platformScale = MathUtil.divHalfUp(platformScaleConfig.getPlatformScale().toString(), "100", 2);
        platformScale = MathUtil.subZeroAndDot(platformScale);
        platformScaleConfig.setPlatformScale(new BigDecimal(platformScale));
        //作者占比
        String authorScale = MathUtil.divHalfUp(platformScaleConfig.getAuthorScale().toString(), "100", 2);
        authorScale = MathUtil.subZeroAndDot(authorScale);
        platformScaleConfig.setAuthorScale(new BigDecimal(authorScale));
        return platformScaleConfigMapper.insertSelective(platformScaleConfig);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return platformScaleConfigMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public PlatformScaleConfig findById(Integer id) {
        return platformScaleConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param platformScaleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(PlatformScaleConfig platformScaleConfig) {
        //平台占比
        String platformScale = MathUtil.divHalfUp(platformScaleConfig.getPlatformScale().toString(), "100", 2);
        platformScale = MathUtil.subZeroAndDot(platformScale);
        platformScaleConfig.setPlatformScale(new BigDecimal(platformScale));
        //作者占比
        String authorScale = MathUtil.divHalfUp(platformScaleConfig.getAuthorScale().toString(), "100", 2);
        authorScale = MathUtil.subZeroAndDot(authorScale);
        platformScaleConfig.setAuthorScale(new BigDecimal(authorScale));
        return platformScaleConfigMapper.updateByPrimaryKeySelective(platformScaleConfig);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<PlatformScaleConfigCustom> findCustomListPage(Page page) {
        List<PlatformScaleConfigCustom> list = platformScaleConfigMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public PlatformScaleConfigCustom findCustomById(Integer id) {
        PlatformScaleConfigCustom custom = platformScaleConfigMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(PlatformScaleConfigCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE,
                custom.getIsDelete(), CommonEnum.class));
        //收益类型（1：文章打赏；2：文章收费）
        custom.setUtilityTypeDesc(EnumUtil.getDescriptionByCodeValue(PlatformScaleConfigEnum.UTILITY_TYPE,
                custom.getUtilityType(), PlatformScaleConfigEnum.class));
        //平台占比
        String platformScale = MathUtil.mulHalfUp(custom.getPlatformScale().toString(), "100", 0);
        custom.setPlatformScale(new BigDecimal(platformScale));
        //作者占比
        String authorScale = MathUtil.mulHalfUp(custom.getAuthorScale().toString(), "100", 0);
        custom.setAuthorScale(new BigDecimal(authorScale));
    }

    /**
     * description：根据收益类型查询唯一数据（一个收益类型只对应一条数据）
     * user 朱正磊
     * time 2019-04-30 14:47
     * @param type 收益类型
     * @return 返回唯一数据
     */
    @Override
    public PlatformScaleConfig findByType(Integer type) {
        return platformScaleConfigMapper.findByType(type);
    }
    
}
